/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yayohos.locationmanager.base;

import com.yayohos.locationmanager.LocationManager;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.listener.LocationListener;
import com.yayohos.locationmanager.providers.locationprovider.DefaultLocationProvider;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.location.Location;

/**
 * LocationBaseFraction
 *
 * @date 2021/5/26
 */
public abstract class LocationBaseFraction extends Fraction implements LocationListener {
    private LocationManager locationManager;

    protected LocationBaseFraction() {
    }

    /**
     * 获取根布局ID
     *
     * @return RootComponent 根布局
     */
    public abstract int getRootComponent();

    /**
     * 初始化组件
     *
     * @param component Component
     */
    public abstract void initComponent(Component component);

    /**
     * 获取LocationConfiguration，用户自定义配置LocationConfiguration
     *
     * @return LocationConfiguration
     */
    public abstract LocationConfiguration getLocationConfiguration();

    /**
     * 获取LocationManager
     *
     * @return locationManager
     */
    protected LocationManager getLocationManager() {
        return locationManager;
    }

    /**
     * 定位开始
     * @noinspection checkstyle:JavadocMethod
     */
    protected void getLocation() {
        if (locationManager != null) {
            locationManager.get();
        } else {
            throw new IllegalStateException("locationManager is null. "
                    + "Make sure you call super.initialize before attempting to getLocation");
        }
    }


    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(getRootComponent(), container, false);
        initComponent(component);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        locationManager = new LocationManager.Builder(getFractionAbility().getApplicationContext())
                .configuration(getLocationConfiguration())
                .locationProvider(new DefaultLocationProvider())
                .ability(getFractionAbility())
                .notify(this)
                .build();
    }

    @Override
    protected void onForeground(Intent intent) {
        locationManager.onForeground(intent);
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        locationManager.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationManager.onStop();
    }

    @Override
    public void onProcessTypeChanged(int processType) {
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onLocationFailed(int type) {
    }

    @Override
    public void onPermissionGranted(boolean alreadyHadPermission) {
    }

    @Override
    public void onStatusChanged(int status) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    /**
     * 用于权限回调
     */
    public interface PermissionCallBack {
        void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults);
    }
}
